package com.sgb.zhaocai.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.zhaocai.R;
import com.sgb.zhaocai.entity.PickExpertDetailEntity;
import com.sgb.zhaocai.widget.adapter.PractisingCertificateAdapter;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PickSelectEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 专家库-执业资格证书
 */
public class PractisingCertificateView extends RelativeLayout {

    private ImageView addCustomField;
    private RecyclerView recyclerview;
    private PractisingCertificateAdapter practisingCertificateAdapter;
    private LinearLayoutManager layoutManager;

    private List<PickExpertDetailEntity.Qualifications> list;

    public PractisingCertificateView(Context context) {
        this(context, null);
    }

    public PractisingCertificateView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public PractisingCertificateView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 加载布局
        LayoutInflater.from(context).inflate(R.layout.zc_practising_certificate_view, this);
        // 获取控件
        recyclerview = findViewById(R.id.recyclerview);//自定义字段的recyclerview
        addCustomField = findViewById(R.id.add_custom_field);//添加自定义字段界面
        addCustomField.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.size() >= 10) {
                    MToast.showToast(context, "最多添加10个执业资格证书");
                } else {
                    //添加数据之前先判断数据是否都有值，处理ui布局
                    dataProcessing(context);
                    //添加view数据之后，处理ui布局
                    addCustomFieldView();
                }
            }
        });
        recyclerviewData(context);
    }

    //集合处理
    private void recyclerviewData(Context context) {
        list = new ArrayList<>();
        if (practisingCertificateAdapter == null) {
            practisingCertificateAdapter = new PractisingCertificateAdapter(context, list);
        }
        layoutManager = new LinearLayoutManager(context);
        recyclerview.setLayoutManager(layoutManager);
        recyclerview.setAdapter(practisingCertificateAdapter);
        practisingCertificateAdapter.setCallback(new PractisingCertificateAdapter.Callback() {
            @Override
            public void delete(int position) {
                list.remove(position);
                //删除view数据之后，处理ui布局
                addCustomFieldView();
            }
        });
    }

    private void addCustomFieldView() {
        practisingCertificateAdapter.notifyDataSetChanged();
        recyclerview.scrollToPosition(list.size() - 1);
    }

    //数据处理
    private void dataProcessing(Context context) {
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (TextUtils.isEmpty(list.get(i).getCertificateName()) ||
                        TextUtils.isEmpty(list.get(i).getNumber()) ||
                        TextUtils.isEmpty(list.get(i).getTime())) {
                    MToast.showToast(context, "请完善执业资格证书");
                    return;
                }
            }
        }
        PickExpertDetailEntity.Qualifications qualifications = new PickExpertDetailEntity.Qualifications();
        list.add(qualifications);
    }

    //获取自定义控件中的数据信息
    public List<PickExpertDetailEntity.Qualifications> getCustomFieldViewData() {
        List<PickExpertDetailEntity.Qualifications> datas = practisingCertificateAdapter.getDatas();
        return datas;
    }

    /**
     * 设置数据
     */
    public void setData(List<PickExpertDetailEntity.Qualifications> qualificationsList) {
        for (PickExpertDetailEntity.Qualifications qualifications : qualificationsList) {
            list.add(qualifications);
        }
        //添加view数据之后，处理ui布局
        addCustomFieldView();
    }

    public void setCertificateData(List<PickSelectEntity> data) {
        practisingCertificateAdapter.setCertificateData(data);
    }
}
